<?php

  // Pour ajouter des équipes
  // auteur : sebastien Mosser
  
class fAddMatche extends aFeature {

   public function __construct() {
      $this->_acl = new AccessControlList();
      $this->_acl->setGranted(7);
      $this->_acl->setGranted(3);
      $this->_acl->setGranted(2);
   }

   public static function getTocEntry() {
      return "Ajouter une Feuille de Match";
   }

   public static function getRealName() {
		return self::getTocEntry();
	}
	
	public static function getSemantic() {
		$res  = "Cette fonctionnalité permet d'ajouter une Feuille de Match dans le système Balloon";
		return $res;
	}


  protected function run() {
  	$action = PropRetriever::getProp($_POST,"action");
  	if ($action == "add")
  		return $this->addMatche();
  	else
  		return $this->displayForm();
	}
	
	
	
	private function addMatche() {

		$check = $this->checkErrors();
		if ($check !== False)
			return $this->displayForm($check);
		
		$matcheNo   = PropRetriever::getProp($_POST,"matcheNo");
		$webNo      = PropRetriever::getProp($_POST,"webNo");
		$day        = PropRetriever::getProp($_POST,"day");
		$matcheDate = PropRetriever::getProp($_POST,"matcheDate");
		$length     = PropRetriever::getProp($_POST,"length");
		$opponent   = 
						ucwords(strtolower(PropRetriever::getProp($_POST,"opponent")));
		$goalsUs    = PropRetriever::getInt($_POST,"goalsUs");
		$goalsThem  = PropRetriever::getInt($_POST,"goalsThem");
		
		$teamId    = PropRetriever::getProp($_POST,"teamId");

		$type      = PropRetriever::getProp($_POST,"type");
		$direction = PropRetriever::getProp($_POST,"direction");
	
		$teamsList     = Team::getTeamsAsList($teamId);
		$typeList      = Matche::gettypeAsList($type);
		$directionList = Matche::getDirectionAsList($direction);

		
		// Récupération de l'équipe
		$aTeam = Team::read($teamId);
		
		// Instanciation du matche
		$aMatche = new Matche();
		$aMatche->setMatchNo($matcheNo);
		$aMatche->setMatchWebNo($webNo);
		$aMatche->setGoalsUs($goalsUs);
		$aMatche->setGoalsThem($goalsThem);
		$aMatche->setOpponent($opponent);
		$aMatche->setDirection($direction);
		$aMatche->setLength($length);
		$aMatche->setDay($day);
		$aMatche->setDate($matcheDate);
		$aMatche->setType($type);
		$aMatche->setGround(-1);
		$aMatche->setGroundType(-1);
		
		// On ajoute
		$matcheRef = $aMatche->create();
		$aTeam->addMatche($matcheRef);
		$aTeam->update();

		// On se déplace sur la gestion des joueurs
		header("Location: sub_managePlayers.php?id=$matcheRef");
	}
	
	private function checkErrors() {
		$matcheDate = PropRetriever::getProp($_POST,"matcheDate");
		$length     = PropRetriever::getProp($_POST,"length");
		$opponent   = PropRetriever::getProp($_POST,"opponent");
		$goalsUs    = PropRetriever::getInt($_POST,"goalsUs");
		$goalsThem  = PropRetriever::getInt($_POST,"goalsThem");
		
		$errors = array();
		
		if ($matcheDate == "")
			$errors["matcheDate"] = "La date du match ne peut être vide !";
		else if (! Date::isValidDate($matcheDate))
   	  $errors["matcheDate"] = 
   	  									"La date du match n'est pas au format [AAAA-MM-JJ] !";
   	
   	if ($opponent == "")
   		$errors["opponent"] = "Vous devez spécifier un adversaire !";
   	
   	if ($goalsUs == "")
   	 	$errors["goalsUs"] = "Le score de l'équipe ne peut être vide !";
   	else if (! is_numeric($goalsUs))
   	 	$errors["goalsUs"] = 
   	 									"Le score de l'équipe n'est pas une valeur numérique !";
		if ($goalsThem == "")
   	 	$errors["goalsThem"] = "Le score de l'adversaire ne peut être vide !";
   	else if (! is_numeric($goalsThem))
   	 	$errors["goalsThem"] = 
   	 							"Le score de l'adversaire n'est pas une valeur numérique !";
		
		if (count($errors) == 0)
			return False;
		else
			return $errors;
	}
	
	private function displayForm($errors = array()) {
		$season = Season::read(Login::seasonChoosen(False));
		$season = $season->displayShort();
	
	
		$matcheNo   = PropRetriever::getProp($_POST,"matcheNo");
		$webNo      = PropRetriever::getProp($_POST,"webNo");
		$day        = PropRetriever::getProp($_POST,"day");
		$matcheDate = PropRetriever::getProp($_POST,"matcheDate");
		$length     = PropRetriever::getProp($_POST,"length");
		$opponent   = 
							ucwords(strtolower(PropRetriever::getProp($_POST,"opponent")));

		$goalsUs    = PropRetriever::getInt($_POST,"goalsUs");
		$goalsThem  = PropRetriever::getInt($_POST,"goalsThem");
		
		$teamId    = PropRetriever::getProp($_POST,"teamId");
		$type      = PropRetriever::getProp($_POST,"type");
		$direction = PropRetriever::getProp($_POST,"direction");
	
		$teamsList     = Team::getTeamsAsList($teamId);
		$typeList      = Matche::gettypeAsList($type);
		$directionList = Matche::getDirectionAsList($direction);
	
		$style = array();
		$style["matcheDate"] = ""; $style["length"] = "";
		$style["opponent"] = "";   $style["goalsUs"] = "";
		$style["goalsThem"] = "";
	
		$errorsText = "";
		if (count($errors) != 0) {
			$errorsText = "<h2> Des erreurs sont survenues ! </h2>";
			$errorsText .= "<p> Veuillez corriger les erreurs suivantes : ";
			$errorsText .= "<ul>";
		}
		
		$errorsArray = array();
		foreach($errors as $k => $v) {
			$style[$k] = "class=\"error\"";
			$errorsText .= "<li> <span class=\"error\"> $v </span> </li>";
		}
			
		if ($errorsText != "")
			$errorsText .= "</ul> </p>";
		
		
		$page = <<<EOS
<h1> Création d'une Feuille de Match pour la saison [ {$season} ] </h1>

<h2> Etape 1/2 : Informations sur le Match </h2>

<div align="center">
<br /> <br />
<form method="post" accept-charset="utf-8" name="addMatche"
			action="?add" >

<input type="hidden" name="action" value="add" />

<table> 

<tr> 
<td align="center">

	<fieldset>
		<legend> Informations Administratives </legend>
		<br />
		
		
		<label> 
			<span>N° de Match : </span>
			<input type="text" name="matcheNo" class="edit" value="${matcheNo}"/>
			&nbsp; &nbsp;
		</label>
		<br />		
		
		<label> 
			<span>Minitel / Web : </span>
			<input type="text" name="webNo" class="edit" value="${webNo}"/>
			&nbsp; &nbsp;
		</label>
		<br />
		
		<label> 
			<span> Journée : </span>
			<input type="text" name="day" class="edit" value="${day}"/>
			&nbsp; &nbsp;
		</label>
		<br />
		
		<label> 
			<span ${style["matcheDate"]}> Date : </span>
			<input type="text" name="matcheDate" class="edit" value="${matcheDate}"
						 onclick="javascript:showCal('MatcheDateCal')" />
			&nbsp; &nbsp;
		</label>
		<br />
		
		<label> 
			<span ${style["length"]}> Durée : </span>
			<input type="text" name="length" class="edit" value="${length}"/>
			&nbsp; &nbsp;
		</label>
		<br />

		<br />
	</fieldset>
	
</td>

<td> &nbsp;&nbsp;&nbsp;&nbsp; </td>

<td align="center">
  <fieldset>
		<legend> Informations Techniques </legend>
		<br />
		
		<label> 
			<span>Équipe : </span>
			<select name="teamId" class="edit" />
			${teamsList}
			</select>
			&nbsp; &nbsp;
		</label>
		<br />
		
		<label> 
			<span ${style["opponent"]}>Adversaire : </span>
			<input type="text" name="opponent" class="edit" value="${opponent}"/>
			&nbsp; &nbsp;
		</label>
		<br />
		
		
		<label> 
			<span>Sens : </span>
			<select name="direction" class="edit" />
			${directionList}
			</select>
			&nbsp; &nbsp;
		</label>
		<br />
		
		<label> 
			<span>Type : </span>
			<select name="type" class="edit" />
			${typeList}
			</select>
			&nbsp; &nbsp;
		</label>
		<br />
		
		<label> 
			<span ${style["goalsUs"]}>Score Club : </span>
			<input type="text" name="goalsUs" class="edit" value="${goalsUs}"/>
			&nbsp; &nbsp;
		</label>
		<br />

		<label> 
			<span ${style["goalsThem"]}>Score Adv. : </span>
			<input type="text" name="goalsThem" class="edit" value="${goalsThem}"/>
			&nbsp; &nbsp;
		</label>
		<br />

		<br /> 
	</fieldset>
</td>
</tr>

<tr>
  <td align="center" colspan="3">
  	<br />
    <input type="submit" value="Valider la Feuille de Match" class="edit" />
  </td>
</tr>
</table>
</div>
</form>

${errorsText}
EOS;

	return $page;
	
	}

}

?>